package educrypt.gui.components;

import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import educrypt.gui.view.CryptanalysisHillView;
import educrypt.i18n.Messages;

class CryptpanalysisHillDialog extends EducryptDialog implements
		CryptanalysisHillView {
	private static final long serialVersionUID = 6161791739809847346L;

	private final JPanel contentPanel = new JPanel();

	private JTextArea txtPlainTextChunk;

	private JSpinner spKeyLength;
	private JButton btnContinue;

	public CryptpanalysisHillDialog(Window owner) {
		super(owner);
		this.setModal(false);

		setTitle(Messages.getString("guiKnownPlaintext"));

		this.setSize(500, 430);
		this.setPreferredSize(contentPanel.getSize());
		getContentPane().setLayout(null);

		JScrollPane scrollPane2 = new JScrollPane((Component) null);
		scrollPane2.setBounds(30, 90, 438, 178);
		getContentPane().add(scrollPane2);

		txtPlainTextChunk = new JTextArea();
		scrollPane2.setViewportView(txtPlainTextChunk);

		JLabel lblKeyLength = new JLabel(Messages.getString("guiKeyLength"));
		lblKeyLength.setBounds(30, 21, 172, 17);
		getContentPane().add(lblKeyLength);

		JLabel lblPlainTextChunk = new JLabel(
				Messages.getString("guiEnterInitialPlainText"));
		lblPlainTextChunk.setBounds(30, 63, 438, 26);
		getContentPane().add(lblPlainTextChunk);

		spKeyLength = new JSpinner(new SpinnerNumberModel(4, 4, 100, 1));

		((JSpinner.DefaultEditor) spKeyLength.getEditor()).getTextField()
				.setEditable(false);

		// WORKAROUND to make spinner display square numbers
		spKeyLength.addChangeListener(new ChangeListener() {
			boolean spNotChange = false;
			int spPrevValue = 4;

			@Override
			public void stateChanged(ChangeEvent e) {

				if (!spNotChange) {
					spNotChange = true;
					double value = ((Integer) spKeyLength.getValue()) * 1.0;

					if (spPrevValue > value) {
						value = value + 1;
						value = Math.sqrt(value) - 1;

					} else if (spPrevValue < value) {
						value = value - 1;
						value = Math.sqrt(value) + 1;
					}

					value = Math.pow(value, 2);
					spPrevValue = (int) value;
					spKeyLength.setValue((int) value);
				} else {
					spNotChange = false;
				}
			}

		});
		// END WORKAROUND

		spKeyLength.setBounds(212, 19, 46, 20);
		getContentPane().add(spKeyLength);

		btnContinue = new JButton(Messages.getString("guiContinue"));
		btnContinue.setBounds(379, 368, 89, 23);
		getContentPane().add(btnContinue);

		JLabel label = new JLabel(
				Messages.getString("guiEnterInitialPlainTextExplanation"));
		label.setBounds(30, 279, 438, 78);
		getContentPane().add(label);
	}

	@Override
	public void open() {
		intitializeValues();
		super.open();
	}

	@Override
	public void open(Component caller) {
		intitializeValues();
		super.open(caller);
	}

	private void intitializeValues() {
		// initializeSpinner();
		this.txtPlainTextChunk.setText("");
	}

	@Override
	public void addListener(ActionListener ctr) {
		btnContinue.setActionCommand(BREAK_HILL);
		btnContinue.addActionListener(ctr);
	}

	@Override
	public String getPlainTextChunk() {
		return txtPlainTextChunk.getText();
	}

	@Override
	public int getKeyLength() {
		Integer value = (Integer) spKeyLength.getValue();
		return value;
	}

	// class MySpinnerModel extends SpinnerNumberModel {
	//
	// private static final long serialVersionUID = -6217603528021127796L;
	//
	// // double value;
	//
	// public MySpinnerModel() {
	// super(2, 2, 10, 1);
	// }
	//
	// @Override
	// public Object getValue() {
	// Object value = super.getValue();
	// return Math.pow((Integer) value, 2);
	// }
	//
	// @Override
	// public Object getNextValue() {
	// Integer nextValue = null;
	// // Double nextValue = value++;
	// if (super.getNextValue() != null) {
	// nextValue = (int) Math.pow((Integer) super.getNextValue(), 2);
	//
	// }
	// return nextValue;
	// }
	//
	// @Override
	// public Object getPreviousValue() {
	// // if (value <= 2) {
	// // return null;
	// // } else {
	// // Double previousValue = value--;
	// // return Math.pow(getpre, 2);
	// //
	// // }
	// Object previousValue = super.getPreviousValue();
	// if (previousValue != null) {
	// previousValue = Math.pow((Integer) previousValue, 2);
	// }
	// return previousValue;
	//
	// }
	//
	// // @Override
	// // public void setValue(Object arg0) {
	// // super.setValue(arg0);
	// // }
	// }
}
